package com.hdx.server.service.impl;





import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hdx.common.constant.MessageConstant;
import com.hdx.common.constant.StatusConstant;
import com.hdx.common.exception.DeletionNotAllowedException;
import com.hdx.common.exception.SetmealEnableFailedException;
import com.hdx.common.result.PageResult;
import com.hdx.model.dto.SetmealDTO;
import com.hdx.model.dto.SetmealPageQueryDTO;
import com.hdx.model.entity.Dish;
import com.hdx.model.entity.Setmeal;
import com.hdx.model.entity.SetmealDish;
import com.hdx.model.vo.SetmealVO;
import com.hdx.server.mapper.DishMapper;
import com.hdx.server.mapper.SetmealDishMapper;
import com.hdx.server.mapper.SetmealMapper;
import com.hdx.server.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;


//根据分类id查询套餐*（name，categoryId，status）套餐id    未知
//
//
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    //套餐分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //Pagehelper
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //Page
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        //PageResult(total,records)
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新增套餐
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        //新增套餐表同时也要新增套餐菜品表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //获取新增套餐id,遍历菜品列表,向套餐菜品口味表插入套餐id和菜品
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList != null && setmealDishList.size() > 0) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.insertBatch(setmealDishList);
        }
    }
    //批量删除套餐
    @Override
    public void deleteBatch(List<Long> ids) {
        //抛出异常:当前套餐处于起售中，不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐表的同时也要删除套餐菜品表
        for (Long id : ids) {
            setmealMapper.deleteById(id);

            setmealDishMapper.deleteBySetmealId(id);
        }
    }

    // 根据id查询套餐
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        //查询套餐表同时查询套餐菜品表
        SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);
        //封装返回setmealVO
        return setmealVO;
    }
    //修改套餐
    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改套餐表基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //删除原来套餐菜品
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //新增套餐菜品信息
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList != null && setmealDishList.size() > 0) {
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
        }
        setmealDishMapper.insertBatch(setmealDishList);

    }
    //套餐起售停售
    @Override
    public void startOrStop(Integer status, Long id) {
        //build()
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        setmealMapper.update(setmeal);

        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(status==StatusConstant.ENABLE) {
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if (dishList != null && dishList.size() > 0) {
                dishList.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }


    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

}
